package cn.wolfcode.gateway.filter;

import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

@Component
public class AuthGlobalFilter implements GlobalFilter {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        /**
         * 前置逻辑
         */
        //获取token
        //从请求中获取token  获取查询参数
        String token = exchange.getRequest().getQueryParams().getFirst("token");
        //用于区分getFirst 的作用 是按参数名获取参数值
        String app = exchange.getRequest().getQueryParams().getFirst("app");
        if (StringUtils.isEmpty(token) || !"123".equals(token) ){
            exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
            return exchange.getResponse().setComplete(); //拦截
        }

            return chain.filter(exchange); //放行

    }
}
